<?php

use yii\helpers\Html;
use yii\helpers\Url;

/**
 * @var $this \yii\web\View
 * @var $manager \yii\web\User
 */
$manager = Yii::$app->get('manager');
?>

<div id="sidebar" class="sidebar responsive sidebar-fixed sidebar-scroll">
    <!--
    <div class="sidebar-shortcuts" id="sidebar-shortcuts">
        <div class="sidebar-shortcuts-large" id="sidebar-shortcuts-large">
            <?php echo Html::a('<i class="ace-icon fa fa-plus"></i>', ['/'], ['class' => 'btn btn-success']); ?>
            <?php echo Html::a('<i class="ace-icon fa fa-book"></i>', ['/'], ['class' => 'btn btn-info']); ?>
            <?php echo Html::a('<i class="ace-icon fa fa-users"></i>', ['/user/admin/index'], ['class' => 'btn btn-warning']); ?>
            <?php echo Html::a('<i class="ace-icon fa fa-signal"></i>', ['/'], ['class' => 'btn btn-danger']); ?>
        </div>
        <div class="sidebar-shortcuts-mini" id="sidebar-shortcuts-mini">
            <?php echo Html::a('', ['/'], ['class' => 'btn btn-success']); ?>
            <?php echo Html::a('', ['/'], ['class' => 'btn btn-info']); ?>
            <?php echo Html::a('', ['/user/admin/index'], ['class' => 'btn btn-warning']); ?>
            <?php echo Html::a('', ['/'], ['class' => 'btn btn-danger']); ?>
        </div>
    </div>
    -->

    <?php
    /**
     * 检查菜单是否需要设置为当前状态
     * @param string $route
     * @return boolean
     */
    function checkMenuActive($route)
    {
        $controller = Yii::$app->controller;
        if (!$controller) {
            return false;
        }
        if ($controller->module->id != 'admin') {
            return false;
        }
        $route = preg_split('/\//', $route);
        if (count($route) == 1) {
            if ($controller->id == $route[0]) {
                return true;
            }
        } elseif (count($route) == 2) {
            if ($controller->id == $route[0] && $controller->action->id == $route[1]) {
                return true;
            }
        }
        return false;
    }
    if (YII_ENV === 'prod') {
        $items = Yii::$app->cache->get('manager_sidebar_items_' . $manager->id);
    } else {
        $items = null;
    }
    if (empty($items)) {
        $items = [
            ['label' => '权限管理', 'rbac' => 'rbac/menu', 'icon' => 'fa fa-lock', 'active' => 'rbac', 'items' => [
                ['label' => '管理员列表', 'rbac' => 'rbac/manager', 'url' => ['/admin/rbac/manager'], 'active' => 'rbac/manager'],
                ['label' => '管理日志', 'rbac' => 'rbac/log', 'url' => ['/admin/rbac/log'], 'active' => 'rbac/log'],
                ['label' => '角色列表', 'rbac' => 'rbac/role', 'url' => ['/admin/rbac/role'], 'active' => 'rbac/role'],
                ['label' => '权限列表', 'rbac' => 'rbac/item', 'url' => ['/admin/rbac/item'], 'active' => 'rbac/item'],
            ]],
            ['label' => '文章管理', 'rbac' => 'article/menu', 'icon' => 'fa fa-newspaper-o', 'active' => 'article', 'items' => [
            ]],
            ['label' => '物联网卡管理', 'rbac' => 'card/menu', 'icon' => 'fa fa-wifi', 'active' => 'card', 'items' => [
                ['label' => '接口列表', 'rbac' => 'card/api', 'url' => ['/admin/card/api-list'], 'active' => 'card/api-list'],
                ['label' => '运营商列表', 'rbac' => 'card/isp', 'url' => ['/admin/card/isp-list'], 'active' => 'card/isp-list'],
                ['label' => '套餐列表', 'rbac' => 'card/package', 'url' => ['/admin/card/package-list'], 'active' => 'card/package-list'],
                // ['label' => 'ISP套餐列表', 'rbac' => 'card/package-isp', 'url' => ['/admin/card/package-isp-list'], 'active' => 'card/package-isp-list'],
                ['label' => '卡列表', 'rbac' => 'card/list', 'url' => ['/admin/card/list'], 'active' => 'card/list'],
                ['label' => '卡类型', 'rbac' => 'card/type', 'url' => ['/admin/card/type'], 'active' => 'card/type'],
                ['label' => '到期提醒卡列表', 'rbac' => 'card/list', 'url' => ['/admin/card/list-end-day-alert', 'search_end_day_alert' => 60], 'active' => 'card/list-end-day-alert'],
                ['label' => '超流量卡列表', 'rbac' => 'card/list', 'url' => ['/admin/card/card-list-overload'], 'active' => 'card/card-list-overload'],
                ['label' => '已到期未停用卡列表', 'rbac' => 'card/list', 'url' => ['/admin/card/over-load-list'], 'active' => 'card/over-load-list'],
                ['label' => '流量超出未停用卡列表', 'rbac' => 'card/list', 'url' => ['/admin/card/flow-over-list'], 'active' => 'card/flow-over-list'],
                ['label' => '卡充值历史列表', 'rbac' => 'card/list', 'url' => ['/admin/card/card-history'], 'active' => 'card/card-history'],
                ['label' => '卡分配', 'rbac' => 'card/dispatch', 'url' => ['/admin/card/dispatch'], 'active' => 'card/dispatch'],
                ['label' => '卡账单', 'rbac' => 'card/bill', 'url' => ['/admin/card/bill'], 'active' => 'card/bill'],
            ]],
            ['label' => '代理管理', 'rbac' => 'agent/menu', 'icon' => 'fa fa-briefcase', 'active' => 'agent', 'items' => [
            ]],
            ['label' => '用户管理', 'rbac' => 'user/menu', 'icon' => 'fa fa-users', 'active' => 'user', 'items' => [
                ['label' => '代理商列表', 'rbac' => 'user/agent-list', 'url' => ['/admin/user/agent-list'], 'active' => 'user/agent-list'],
                ['label' => '代理商日志', 'rbac' => 'user/agent-log', 'url' => ['/admin/user/agent-log-list'], 'active' => 'user/agent-log-list'],
                ['label' => '绑定imei', 'rbac' => 'user/bind-imei-list', 'url' => ['/admin/user/bind-imei-list'], 'active' => 'user/bind-imei-list'],
                ['label' => '用户列表', 'rbac' => 'user/user-list', 'url' => ['/admin/user/user-list'], 'active' => 'user/user-list'],
                ['label' => '用户日志', 'rbac' => 'user/user-log', 'url' => ['/admin/user/user-log-list'], 'active' => 'user/user-log-list'],
            ]],
            ['label' => '入库管理', 'rbac' => 'storage/menu', 'icon' => 'fa fa-adjust', 'active' => 'storage', 'items' => [
                ['label' => '入库订单',  'url' => ['/admin/storage/list'], 'active' => 'storage/list'],
                ['label' => '入库列表',  'url' => ['/admin/storage/order'], 'active' => 'storage/order'],
            ]],
            ['label' => '出库管理', 'rbac' => 'outbound/menu', 'icon' => 'fa fa-users', 'active' => 'outbound', 'items' => [
                ['label' => '订单列表',  'url' => ['/admin/outbound/order-list'], 'active' => 'outbound/order-list'],
                ['label' => '出库列表',  'url' => ['/admin/outbound/list'], 'active' => 'outbound/list'],
                
            ]],
            ['label' => '财务管理', 'rbac' => 'finance/menu', 'icon' => 'fa fa-dollar', 'active' => 'finance', 'items' => [
                ['label' => '卡充值列表', 'rbac' => 'finance/user-card-recharge', 'url' => ['/admin/finance/user-card-recharge-list'], 'active' => 'finance/user-card-recharge-list'],
                ['label' => '卡预充值列表', 'rbac' => 'finance/user-card-reserve-recharge', 'url' => ['/admin/finance/user-card-reserve-recharge-list'], 'active' => 'finance/user-card-reserve-recharge-list'],
                ['label' => '代理商收益', 'rbac' => 'finance/agent-profit', 'url' => ['/admin/finance/agent-profit'], 'active' => 'finance/agent-profit'],
                ['label' => '代理商激活奖励', 'rbac' => 'finance/active-reward', 'url' => ['/admin/finance/active-reward'], 'active' => 'finance/active-reward'],
                ['label' => '代理商佣金', 'rbac' => 'finance/agent-commission', 'url' => ['/admin/finance/agent-commission'], 'active' => 'finance/agent-commission'],
                ['label' => '批量支付代理商佣金', 'rbac' => 'finance/multi-commission', 'url' => ['/admin/finance/multi-commission'], 'active' => 'finance/multi-commission'],
                ['label' => '代理商付款记录', 'rbac' => 'finance/payment-list', 'url' => ['/admin/finance/payment-list'], 'active' => 'finance/payment-list'],
                ['label' => '充值退款记录', 'rbac' => 'finance/refund-list', 'url' => ['/admin/finance/refund-list'], 'active' => 'finance/refund-list'],
                ['label' => '视频会员卡密', 'rbac' => 'finance/membership-key', 'url' => ['/admin/finance/membership-key'], 'active' => 'finance/membership-key'],

            ]],
            ['label' => '系统统计', 'rbac' => 'analyse', 'icon' => 'fa fa-area-chart', 'active' => 'analyse', 'items' => [
                ['label' => '充值分析',  'url' => ['/admin/analyse/recharge-analyse'], 'active' => 'analyse/recharge-analyse'],
                ['label' => '充值套餐分析',  'url' => ['/admin/analyse/recharge-package-analyse'], 'active' => 'analyse/recharge-package-analyse'],
                ['label' => '数据用量分析',  'url' => ['/admin/analyse/average-usage'], 'active' => 'analyse/average-usage'],
                ['label' => '等待激活卡列表',  'url' => ['/admin/analyse/wait-active-card-list'], 'active' => 'analyse/wait-active-card-list'],
            ]],
            ['label' => '消息管理', 'rbac' => 'message/menu', 'icon' => 'fa fa-adjust', 'active' => 'message', 'items' => [
                ['label' => '消息推送', 'rbac' => 'message/push-message', 'url' => ['/admin/message/push-message'], 'active' => 'message/push-message'],

            ]],
            ['label' => '系统管理', 'rbac' => 'system/menu', 'icon' => 'fa fa-cogs', 'active' => 'system', 'items' => [
                ['label' => '系统设置', 'rbac' => 'system/config', 'url' => ['/admin/system/config'], 'active' => 'system/config'],
                ['label' => '定时任务', 'rbac' => 'system/task', 'url' => ['/admin/system/task'], 'active' => 'system/task'],
                ['label' => '错误日志', 'rbac' => 'system/error', 'url' => ['/admin/system/error'], 'active' => 'system/error'],
                ['label' => '接口版本', 'rbac' => 'system/api-version', 'url' => ['/admin/system/api-version'], 'active' => 'system/api-version'],
                ['label' => '接口客户端', 'rbac' => 'system/api-client', 'url' => ['/admin/system/api-client'], 'active' => 'system/api-client'],
            ]],
        ];
        // 检查权限
        array_walk($items, function (&$item) use ($manager) {
            /** @var $manager \yii\web\User */
            if (isset($item['rbac']) && !$manager->can($item['rbac'])) {
                $item = null;
                return;
            }
            if (isset($item['items']) && is_array($item['items'])) {
                array_walk($item['items'], function (&$item) use ($manager) {
                    /** @var $manager \yii\web\User */
                    if (isset($item['rbac']) && !$manager->can($item['rbac'])) {
                        $item = null;
                        return;
                    }
                });
            }
        });
        Yii::$app->cache->set('manager_sidebar_items_' . $manager->id, $items, 3600);
    }?>

    <ul class="nav nav-list">
        <li class="">
            <a href="<?php echo Url::to(['/admin']); ?>">
                <i class="menu-icon fa fa-tachometer"></i>
                <span class="menu-text"> 控制台</span>
            </a>

            <b class="arrow"></b>
        </li>
        <?php foreach ($items as $item) {
            if (empty($item)) {
                continue;
            } ?>
            <li class="<?php if (checkMenuActive($item['active'])) {
                echo 'open active';
            } ?>">
                <a href="#" class="dropdown-toggle">
                    <i class="menu-icon <?php echo $item['icon']; ?>"></i>
                    <span class="menu-text"><?php echo $item['label']; ?></span>
                    <b class="arrow fa fa-angle-down"></b>
                </a>
                <b class="arrow"></b>
                <ul class="submenu">
                    <?php if (isset($item['items']) && is_array($item['items'])) {
                        foreach ($item['items'] as $subitem) {
                            if (empty($subitem)) {
                                continue;
                            } ?>
                            <li class="<?php if (checkMenuActive($subitem['active'])) {
                                echo 'active';
                            } ?>">
                                <a href="<?php echo Url::to($subitem['url']); ?>">
                                    <i class="menu-icon fa fa-caret-right"></i>
                                    <?php echo $subitem['label']; ?>
                                </a>
                                <b class="arrow"></b>
                            </li>
                        <?php }
                    } ?>
                </ul>
            </li>
        <?php } ?>

    </ul>
    <div class="sidebar-toggle sidebar-collapse" id="sidebar-collapse">
        <i class="ace-icon fa fa-angle-double-left" data-icon1="ace-icon fa fa-angle-double-left"
           data-icon2="ace-icon fa fa-angle-double-right"></i>
    </div>
    <script>
        try {
            ace.settings.check('sidebar', 'collapsed');
        } catch (e) {
        }
    </script>
</div>

